package com.arunsawad.ilertu.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.arunsawad.ilertu.R;
import com.arunsawad.ilertu.common.Constants;
import com.arunsawad.ilertu.common.Validator;

public class Settings extends Activity {
	SharedPreferences preferences;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.act_settings);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				R.layout.title0_imgbtn_txt);
		
		initTitle();
		
		preferences = getSharedPreferences(Constants.PREF_FILE, MODE_PRIVATE);
		Validator.Validate(this, preferences);

		String[] settings = new String[] { getString(R.string.about_arunsawad),
				getString(R.string.about_do_d),
				getString(R.string.tell_a_friend), getString(R.string.language) };
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, settings);
		ListView listView = (ListView) findViewById(R.id.settings_lv);
		listView.setAdapter(adapter);

		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent;
				switch (arg2) {
				case 0:
					intent = new Intent(Intent.ACTION_VIEW, Uri
							.parse("http://ilertu.com/"));
					startActivity(intent);
					break;
				case 1:
					intent = new Intent(Intent.ACTION_VIEW, Uri
							.parse("http://www.do-d-club.com/ilertu/"));
					startActivity(intent);
					break;
				case 2:
					AlertDialog.Builder alertDialogBuilder = new Builder(
							Settings.this);
					alertDialogBuilder
							.setTitle(getString(R.string.account_options));
					String[] types = { getString(R.string.sms),
							getString(R.string.email),
							getString(R.string.cancel) };
					alertDialogBuilder.setItems(types, new OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
							Intent intent;
							switch (which) {
							case 0: // sms
								intent = new Intent(Intent.ACTION_VIEW);
								intent.putExtra(
										"sms_body",
										getString(R.string.tell_a_friend_message));
								intent.setType("vnd.android-dir/mms-sms");
								startActivity(intent);
								break;
							case 1: // email
								intent = new Intent(Intent.ACTION_SEND);
								intent.setType("message/rfc822");
								intent.putExtra(
										Intent.EXTRA_SUBJECT,
										getString(R.string.tell_a_friend_subject));
								intent.putExtra(
										Intent.EXTRA_TEXT,
										getString(R.string.tell_a_friend_message));
								try {
									startActivity(Intent.createChooser(intent,
											"Send mail..."));
								} catch (android.content.ActivityNotFoundException ex) {
									Toast.makeText(
											Settings.this,
											getString(R.string.no_email_client),
											Toast.LENGTH_SHORT).show();
								}
								break;
							case 2:
								dialog.cancel();
								break;
							}
						}
					});
					alertDialogBuilder.show();
					break;
				case 3:
					intent = new Intent(Settings.this, Languages.class);
					startActivity(intent);
					break;
				}
			}
		});
	}

	public void initTitle(){
		ImageButton imgBtn1 = (ImageButton) findViewById(R.id.title0_imgBtn1);
		imgBtn1.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(), Home.class);
				startActivity(intent);
			}
		});
		
		TextView txtTitle = (TextView) findViewById(R.id.title0_txtTitle);
		txtTitle.setText(getString(R.string.settings));
	}
	
	// {{ onClick events
	public void goToArunsawad(View view) {
		Intent intent = new Intent(Intent.ACTION_VIEW,
				Uri.parse("http://ilertu.com/"));
		startActivity(intent);
	}

	public void goToDoD(View view) {
		Intent intent = new Intent(Intent.ACTION_VIEW,
				Uri.parse("http://www.do-d-club.com/ilertu/"));
		startActivity(intent);
	}

	public void tellAFriend(View view) {
		AlertDialog.Builder alertDialogBuilder = new Builder(this);
		alertDialogBuilder.setTitle(getString(R.string.account_options));
		String[] types = { getString(R.string.sms), getString(R.string.email),
				getString(R.string.cancel) };
		alertDialogBuilder.setItems(types, new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				Intent intent;
				switch (which) {
				case 0: // sms
					intent = new Intent(Intent.ACTION_VIEW);
					intent.putExtra("sms_body",
							getString(R.string.tell_a_friend_message));
					intent.setType("vnd.android-dir/mms-sms");
					startActivity(intent);
					break;
				case 1: // email
					intent = new Intent(Intent.ACTION_SEND);
					intent.setType("message/rfc822");
					intent.putExtra(Intent.EXTRA_SUBJECT,
							getString(R.string.tell_a_friend_subject));
					intent.putExtra(Intent.EXTRA_TEXT,
							getString(R.string.tell_a_friend_message));
					try {
						startActivity(Intent.createChooser(intent,
								"Send mail..."));
					} catch (android.content.ActivityNotFoundException ex) {
						Toast.makeText(Settings.this,
								getString(R.string.no_email_client),
								Toast.LENGTH_SHORT).show();
					}
					break;
				case 2:
					dialog.cancel();
					break;
				}
			}
		});
		alertDialogBuilder.show();
	}

	public void setLanguage(View view) {
		Intent intent = new Intent(this, Languages.class);
		startActivity(intent);
	}
	// }}

	// {{ callback
	// }}
}